package top.iridescent.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import top.iridescent.constant.ResultMsg;
import top.iridescent.db.annotation.DataSource;
import top.iridescent.dao.MhEquipmentInfoDao;
import top.iridescent.dao.model.request.MhEquipmentInfoQuery;
import top.iridescent.entity.*;
import top.iridescent.entity.model.res.ZBRes;
import top.iridescent.model.PageModel;
import top.iridescent.model.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("mhEquipmentInfoService")
//@DataSource("mh_game_local")
public class MhEquipmentInfoServiceImpl extends ServiceImpl<MhEquipmentInfoDao, MhEquipmentInfo> {
    @Autowired
    private MhIncomeServiceImpl incomeService;
    @Autowired
    private MhBaseAttributeServiceImpl baseAttributeService;
    @Autowired
    private MhExpandAttributeServiceImpl expandAttributeService;

    public Result findPage(Page<Map> page, MhEquipmentInfoQuery param) {
        IPage<MhEquipmentInfo> pageData = baseMapper.findPage(page, param);
        if (pageData != null) {
            pageData.getRecords().forEach(e -> {
                handleDetail(e);
            });
            return Result.ok().setData(new PageModel(pageData.getRecords(), pageData.getTotal(), pageData.getSize(), pageData.getCurrent()));
        } else {
            return Result.error(ResultMsg.DATA_NOT_EXIT);
        }
    }

    public Result getById(Long id) {
        MhEquipmentInfo data = baseMapper.findDetailById(id);
        handleDetail(data);
        return Result.ok().setData(data);
    }

    public Result saveInfo(MhEquipmentInfo param) {
        incomeService.saveInfo(new MhIncome("o",param.getPrice().doubleValue(),param.getName()));
        MhBaseAttribute baseAttr = param.getBaseAttr();
        baseAttributeService.save(baseAttr);
        param.setBaseAttrId(baseAttr.getId());
        MhExpandAttribute expandAttr = param.getExpandAttr();
        expandAttributeService.save(expandAttr);
        param.setExpandAttrId(expandAttr.getId());
        return Result.ok().setData(super.save(param));
    }

    public Result update(MhEquipmentInfo param) {
        MhEquipmentInfo data = super.getById(param.getId());
        baseAttributeService.updateById(param.getBaseAttr());
        expandAttributeService.updateById(param.getExpandAttr());
        if (ObjectUtils.isEmpty(data)) {
            return Result.error(ResultMsg.DATA_NOT_EXIT);
        }
        return Result.ok().setData(super.updateById(param));
    }

    public Result deleteById(Long id) {
        MhEquipmentInfo data = super.getById(id);
        baseAttributeService.removeById(data.getBaseAttrId());
        expandAttributeService.removeById(data.getExpandAttrId());
        return Result.ok().setData(super.removeById(id));
    }

    public void handleDetail(MhEquipmentInfo data) {
        data.setBaseAttr(baseAttributeService.getById(data.getBaseAttrId()));
        data.setExpandAttr(expandAttributeService.getById(data.getExpandAttrId()));
    }

    public Result getNoUsedEQ(Long id) {
        List<MhEquipmentInfo> list = list();

        Map<Long, List<MhEquipmentInfo>> map = list.stream().collect(Collectors.groupingBy(MhEquipmentInfo::getType));
        ZBRes res = new ZBRes();
        res.setWu(map.get(1l));
        res.setXie(map.get(6l));
        res.setYao(map.get(5l));
        res.setShi(map.get(4l));
        if (0l==id) {
            res.setTou(map.get(7l));
            res.setYi(map.get(8l));
        } else {
            res.setTou(map.get(2l));
            res.setYi(map.get(3l));
        }
        return Result.ok().setData(res);
    }
}